home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-25 | 5.2 KB | 159 lines | [TEXT/CWIE] |
- //================================================================================
- // Greg Anderson
- // db+
- //
- // Abstract base class for objects that can have update pointers, participate
- // in transactions, and so on.
- // 16, 17 May 1994
- //================================================================================
- #pragma once
-
- #ifndef __TRANSACTIONAWAREOBJECT__
- #define __TRANSACTIONAWAREOBJECT__
-
- #include "Int64.h"
-
- class TTransaction;
- // class TAbstractUpdatePointer;
-
- class TDBRecord;
- class TDBElement;
- class TDBProperty;
- class TDataRecord;
-
- const OSErr eAlreadyInTransaction = -31222;
- const OSErr eNotInTransaction = -31223;
-
- #define kNoTransaction nil
-
- //================================================================================
- // class TTransactionBaseObject
- //
- // Both update pointers and transaction-aware objects are derived from
- // the class TTransactionBaseObject.
- //================================================================================
- class TTransactionBaseObject
- {
- //
- // TAbstractUpdatePointer is a friend class because only the update pointer
- // is allowed to call the commit / discard changes method of an object
- //
- friend class TAbstractUpdatePointer;
-
- private:
- long fReferenceCount;
-
- public:
- TTransactionBaseObject() : fReferenceCount(0) {};
- virtual ~TTransactionBaseObject() {};
-
- //
- // The object key and object key space are used to uniquely
- // define an object, so that a transaction may determine if
- // it already has an update pointer for a given object.
- //
- virtual Int64 ObjectsKeySpace() const = 0;
- virtual long ObjectKey() const = 0;
-
- //
- // Convenience function for comparing keys:
- //
- Boolean KeysMatch(const TTransactionBaseObject& test) const
- {
- return (this->ObjectsKeySpace() == test.ObjectsKeySpace()) && (this->ObjectKey() == test.ObjectKey());
- };
-
-
- // void AddReference() const { ++((TTransactionBaseObject*)this)->fReferenceCount; }
- // void RemoveReference() const { --((TTransactionBaseObject*)this)->fReferenceCount; };
- virtual void AddReference() const;
- virtual Boolean RemoveReference() const;
- Boolean HasReference() const { return fReferenceCount > 0; }
-
- //
- // Delete this if we can
- //
- virtual void DisposeRecordIfUnreferenced();
-
- protected:
- //
- // A transaction will call either commit changes or discard changes,
- // but never both.
- //
- virtual void PrepareToCommit(TTransaction* transaction);
- virtual void CommitChanges(TTransaction* transaction) = 0;
- virtual void DiscardChanges(TTransaction* transaction) = 0;
- };
-
- //================================================================================
- // class TTransactionAwareObject
- //
- // Any object that can create an update pointer and be part of a transaction
- // is derived from the class TTransactionAwareObject
- //================================================================================
- class TTransactionAwareObject : public TTransactionBaseObject
- {
- //
- // TTransaction is a friend class because only the transaction
- // is allowed to ask for the update pointer for an object.
- //
- friend class TTransaction;
-
- private:
- TTransaction* fTransaction;
-
- public:
- TTransactionAwareObject() : fTransaction(nil) {};
- virtual ~TTransactionAwareObject() {};
-
- Boolean InTransaction() const { return fTransaction != nil; };
- Boolean InTransaction(TTransaction* t) const { return fTransaction == t; };
-
- //
- // This method should really be protected
- //
- TTransaction* Transaction() const { return fTransaction; };
-
- //
- // Objects that can belong to transactions must be able
- // to create an update pointer and commit and discard changes;
- // However, only their friends can ask them to do it (in this
- // case, only a transaction object will call these methods).
- //
- // IMPORTANT: Note that the following methods are all declared
- // to be 'const', even though they change fields of the object.
- // It is very important that these methods be declared const so
- // that cursors with const-only access can call them. It isn't
- // too terrible to call these methods 'const', though, since they
- // are only changing cached references (to the object's transaction
- // and update pointer), and are not actually changing the state
- // of the data the object represents.
- //
- protected:
- void SetTransaction(TTransaction* t) const { ((TTransactionAwareObject*)this)->fTransaction = t; }
-
- //
- // GrabUpdatePointer should create an update pointer, or fail if one already exists
- // GetUpdatePointer should return the update pointer that already exists for the object, if any
- // ReleaseUpdatePointer is called by the transaction after CommitChanges or DiscardChanges
- // is called, but before the update pointer is actually deleted.
- //
- virtual TTransactionAwareObject* GrabUpdatePointer(TTransaction* transaction) const;
-
- #if 0
- virtual const TDBRecord* AbstractDBRecord() const;
- virtual const TDBElement* DBElementRecord() const;
- virtual const TDBProperty* DBPropertyRecord() const;
- virtual const TDataRecord* DataRecord() const;
- #endif
-
- virtual TDBRecord* AbstractDBRecord() const;
- virtual TDBElement* DBElementRecord() const;
- virtual TDBProperty* DBPropertyRecord() const;
- virtual TDataRecord* DataRecord() const;
-
- };
-
-
- #endif
-